package com.qymerge.manage.base.service.impl;

import com.qymerge.manage.base.dao.IMetaProfitTableTemplateDao;
import com.qymerge.manage.base.ro.MetaProfitTableTemplateRO;
import com.qymerge.manage.base.service.IMetaProfitTableTemplateService;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>Title: MetaProfitTableTemplateServiceImpl</p>
 * <p>Description: 利润表模板定义表</p>
 *
 * @author Xiao
 * @date 2025/4/29 18:43
 * @version 1.0.0
 */
@Service
public class MetaProfitTableTemplateServiceImpl implements IMetaProfitTableTemplateService {

    @Autowired
    private IMetaProfitTableTemplateDao metaProfitTableTemplateDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(MetaProfitTableTemplateRO ro, PageRequest pageRequest) {
        List list = metaProfitTableTemplateDao.list(ro, pageRequest);
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = metaProfitTableTemplateDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MetaProfitTableTemplateRO detail(Long id) {
        return metaProfitTableTemplateDao.findOne(id);
    }

    @Override
    public ActionResultRO save(MetaProfitTableTemplateRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = metaProfitTableTemplateDao.insert(ro);
        } else {
            rn = metaProfitTableTemplateDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = metaProfitTableTemplateDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"成功", "删除" + rn +"失败");
    }

    @Override
    public ActionResultRO updateActive(long id, int status, String desc) {
        int rn = metaProfitTableTemplateDao.updateActive(id, status);
        return new ActionResultRO(rn, desc + "成功", desc + "失败");
    }

    @Override
    public MetaProfitTableTemplateRO active() {
        return metaProfitTableTemplateDao.findOne();
    }


}
